package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.domain.SysRegion;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMechanismMapper;
import com.ruoyi.system.domain.SysMechanism;
import com.ruoyi.system.service.ISysMechanismService;

/**
 * 机构信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class SysMechanismServiceImpl implements ISysMechanismService 
{
    @Autowired
    private SysMechanismMapper sysMechanismMapper;
    @Autowired
    private SysRegionMapper sysRegionMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询机构信息管理
     * 
     * @param id 机构信息管理主键
     * @return 机构信息管理
     */
    @Override
    public SysMechanism selectSysMechanismById(Long id)
    {
        SysMechanism sysMechanism = sysMechanismMapper.selectSysMechanismById(id);
        List<String> city = Arrays.asList(sysMechanism.getRegion().split("-"));
        String districtName = city.get(2);
        SysRegion sysRegion = sysRegionMapper.selectSysRegionByDistrictName(districtName);
        List<String> cityCode=new ArrayList<>();
        cityCode.add(sysRegion.getProvinceCode());
        cityCode.add(sysRegion.getCityCode());
        cityCode.add(sysRegion.getDistrictCode());
        sysMechanism.setCityCode(cityCode);
        return sysMechanism;
    }

    /**
     * 查询机构信息管理列表
     * 
     * @param sysMechanism 机构信息管理
     * @return 机构信息管理
     */
    @Override
    public List<SysMechanism> selectSysMechanismList(SysMechanism sysMechanism)
    {
        return sysMechanismMapper.selectSysMechanismList(sysMechanism);
    }

    /**
     * 新增机构信息管理
     * 
     * @param sysMechanism 机构信息管理
     * @return 结果
     */
    @Override
    public int insertSysMechanism(SysMechanism sysMechanism)
    {
        sysMechanism.setCreateTime(DateUtils.getNowDate());
        SysDept sysDept=new SysDept();
        sysDept.setParentId(sysMechanism.getParentId());
        sysDept.setDeptName(sysMechanism.getMechanismName());
        sysDeptMapper.insertDept(sysDept);
        return sysMechanismMapper.insertSysMechanism(sysMechanism);
    }

    /**
     * 修改机构信息管理
     * 
     * @param sysMechanism 机构信息管理
     * @return 结果
     */
    @Override
    public int updateSysMechanism(SysMechanism sysMechanism)
    {
        SysDept sysDept=new SysDept();
        sysDept.setDeptName(sysMechanism.getMechanismName());
        sysDept.setParentId(sysMechanism.getParentId());
        sysDept.setDeptId(sysMechanism.getId());
        sysDeptMapper.updateDept(sysDept);
        return sysMechanismMapper.updateSysMechanism(sysMechanism);
    }

    /**
     * 批量删除机构信息管理
     * 
     * @param ids 需要删除的机构信息管理主键
     * @return 结果
     */
    @Override
    public int deleteSysMechanismByIds(Long[] ids)
    {
        for (Long id : ids) {
            sysDeptMapper.deleteDeptById(id);
        }
        return sysMechanismMapper.deleteSysMechanismByIds(ids);
    }

    /**
     * 删除机构信息管理信息
     * 
     * @param id 机构信息管理主键
     * @return 结果
     */
    @Override
    public int deleteSysMechanismById(Long id)
    {
        sysDeptMapper.deleteDeptById(id);
        return sysMechanismMapper.deleteSysMechanismById(id);
    }

    @Override
    public Long selectMaxId() {
        return sysMechanismMapper.selectMaxId();
    }
}
